package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.TeacherDto;
import com.zeny.coding.entity.Teacher;
import com.zeny.coding.entity.TeacherExample;
import com.zeny.coding.mapper.TeacherMapper;
import com.zeny.coding.service.TeacherService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <h2>讲师服务</h2>
 *
 * @Author Zeny
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void list(PageDto<TeacherDto> pageDto) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.setOrderByClause("id desc");
        //查询结果
        List<Teacher> teacherList = this.teacherMapper.selectByExample(teacherExample);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<TeacherDto> teacherDtoList = new ArrayList<>();
        for (Teacher teacher : teacherList) {
            TeacherDto teacherDto = new TeacherDto();
            BeanUtils.copyProperties(teacher, teacherDto);
            teacherDtoList.add(teacherDto);
        }
        //设置记录数
        pageDto.setList(teacherDtoList);
    }

    @Override
    public void save(TeacherDto teacherDto) {
        //DTO转~
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        teacher.setId(String.valueOf(snowFlake.nextId()));
        this.teacherMapper.insertSelective(teacher);
    }

    @Override
    public void update(TeacherDto teacherDto) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        this.teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    public TeacherDto findById(String id) {
        Teacher teacher = this.teacherMapper.selectByPrimaryKey(id);
        TeacherDto teacherDto = new TeacherDto();
        BeanUtils.copyProperties(teacher, teacherDto);
        return teacherDto;
    }

    @Override
    public void deleteById(String id) {
        this.teacherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TeacherDto> findAll() {
        List<Teacher> teachers = this.teacherMapper.selectByExample(null);
        List<TeacherDto> teacherDtoList = new ArrayList<>();
        for (Teacher teacher : teachers) {
            TeacherDto teacherDto = new TeacherDto();
            BeanUtils.copyProperties(teacher, teacherDto);
            teacherDtoList.add(teacherDto);
        }
        return teacherDtoList;
    }
}
